package com.xq.shop.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;

public class FtpUtil
{

  private FTPClient ftp = null;

  public FtpUtil(String server, int port, String username, String passwd)
    throws Exception
  {
    this.ftp = new FTPClient();
    try {
      this.ftp.connect(server, port);
      this.ftp.login(username, passwd);
    } catch (Exception e) {
      throw e;
    }
  }

  public boolean disConnect()
  {
    if (this.ftp != null) {
      try {
        this.ftp.disconnect();
        return true;
      } catch (IOException e) {
        return false;
      }
    }
    return true;
  }

  public boolean deleteFile(String fileName)
  {
    try
    {
      return this.ftp.deleteFile(fileName);
    } catch (IOException e) {
    }return false;
  }

  public List<String> getFileList(String path)
  {
    List<String> list = new ArrayList<String>();
    this.ftp.setControlEncoding("GBK");
    FTPFile[] flist = null;
    try {
      flist = this.ftp.listFiles(path);
      for (int i = 0; i < flist.length; i++)
        if (!flist[i].isDirectory())
          list.add(flist[i].getName());
    }
    catch (IOException e)
    {
    }

    return list;
  }

  public boolean upload(String remoteDir, String remoteFileName, File localFile)
  {
    InputStream stream = null;
    try {
      stream = new FileInputStream(localFile);

      this.ftp.enterLocalPassiveMode();
      this.ftp.setFileType(2);
      this.ftp.changeWorkingDirectory(remoteDir);
      return this.ftp.storeFile(remoteDir + remoteFileName, stream);
    } catch (Exception e) {
      return false;
    } finally {
      try {
        if (stream != null)
          stream.close();
      }
      catch (IOException e) {
        e.printStackTrace();
      }
    }
  }

  public boolean download(String remoteDir, String remoteFileName, String localDir)
  {
    File file = new File(localDir + remoteFileName);
    OutputStream out = null;
    try {
      if (!file.exists()) {
        file.createNewFile();
      }
      out = new FileOutputStream(file);
      this.ftp.setFileType(2);
      this.ftp.retrieveFile(remoteDir + remoteFileName, out);
      return true;
    } catch (Exception e) {
      return false;
    } finally {
      try {
        out.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }

  public static void writeXmlToFile(String xml, String fileName)
  {
    try
    {
      File f = new File(fileName);
      if (f.exists()) {
        f.delete();
      }
      f.createNewFile();
      BufferedWriter output = new BufferedWriter(new FileWriter(f));
      output.write(xml);
      output.close();
    } catch (Exception e) {
    }
  }
}
